{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9fd54a32",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/openai.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e3a8796-edc8-43f2-94ad-fe4fb20d70ed",
   "metadata": {},
   "source": [
    "# Fireworks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "081d07d2",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f6f8702",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index llama-index-llms-fireworks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b007403c-6b7a-420c-92f1-4171d05ed9bb",
   "metadata": {},
   "source": [
    "## Basic Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83bfb4fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.fireworks import Fireworks\n",
    "\n",
    "# For updated list of available models see: https://app.fireworks.ai/models\n",
    "llm = Fireworks(\n",
    "    model=\"accounts/fireworks/models/llama-v3p1-8b-instruct\",\n",
    "    # api_key=\"some key\",  # uses FIREWORKS_API_KEY env var by default\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ead155e-b8bd-46f9-ab9b-28fc009361dd",
   "metadata": {},
   "source": [
    "#### Call `complete` with a prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60be18ae-c957-4ac2-a58a-0652e18ee6d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.complete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac2cbebb-a444-4a46-9d85-b265a3483d68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A well-known figure in the tech industry and entrepreneurship world. Paul Graham is a:\n",
      "\n",
      "1. **Venture capitalist**: He co-founded Y Combinator (YC), a startup accelerator and seed fund that has invested in many successful companies, including Airbnb, Dropbox, and Reddit.\n",
      "2. **Entrepreneur**: Graham co-founded Viaweb, an online auction company that was acquired by Yahoo! in 1998 for $49 million.\n",
      "3. **Author**: He has written several books on entrepreneurship, including \"Hackers & Painters: Big Ideas from the Computer Age\" and \"The Startup Owner's Manual\".\n",
      "4. **Blogger**: Graham writes a popular blog, \"Paul Graham's Essays\", where he shares his thoughts on entrepreneurship, startups, and technology.\n",
      "5. **Philosopher**: His writings often explore the intersection of technology, economics, and philosophy, and he has been influenced by thinkers like Friedrich Hayek and Ayn Rand.\n",
      "\n",
      "Graham is known for his contrarian views on entrepreneurship and his emphasis on the importance of experimentation, iteration, and learning from failure. He has also been a vocal advocate for the idea that startups should focus on building a product that solves a real problem, rather than trying to create a \"unicorn\" company.\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14831268-f90f-499d-9d86-925dbc88292b",
   "metadata": {},
   "source": [
    "#### Call `chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbe29574-4af1-48d5-9739-f60652b6ce6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
    "]\n",
    "resp = llm.chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cbd550a-0264-4a11-9b2c-a08d8723a5ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: Me hearty! Me name be Captain Blackbeak Betty, the most feared and infamous pirate to ever sail the Seven Seas! Me be a swashbucklin' scallywag with a heart o' gold and a spirit o' adventure. Me ship, the \"Maverick's Revenge\", be me home and me pride, and me crew be the most loyal and trusty scurvy dogs on the high seas!\n",
      "\n",
      "Now, what be bringin' ye to these fair waters? Are ye lookin' to join me crew and sail the seas in search o' treasure and glory? Or be ye just lookin' for a bit o' trouble and a taste o' the pirate life? Either way, ye be in luck, matey, 'cause Captain Blackbeak Betty be here to guide ye through the choppiest o' waters and the most treacherous o' seas!\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ed5e894-4597-4911-a623-591560f72b82",
   "metadata": {},
   "source": [
    "## Streaming"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cb7986f-aaed-42e2-abdd-f274f6d4fc59",
   "metadata": {},
   "source": [
    "Using `stream_complete` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d43f17a2-0aeb-464b-a7a7-732ba5e8ef24",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.stream_complete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0214e911-cf0d-489c-bc48-9bb1d8bf65d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A well-known figure in the tech industry!\n",
      "\n",
      "Paul Graham is a British-American programmer, venture capitalist, and essayist. He is best known for:\n",
      "\n",
      "1. **Co-founding Y Combinator**: In 2005, Graham co-founded Y Combinator, a startup accelerator that provides seed funding and mentorship to early-stage startups. Y Combinator has become one of the most successful and influential startup accelerators in the world.\n",
      "2. **Writing essays on entrepreneurship and programming**: Graham is a prolific writer and has written many essays on topics such as entrepreneurship, programming, and the startup ecosystem. His essays are widely read and respected in the tech industry.\n",
      "3. **Being a successful entrepreneur**: Before co-founding Y Combinator, Graham co-founded Viaweb, an online auction company that was acquired by Yahoo! in 1998 for $49 million.\n",
      "4. **Investing in startups**: Graham has invested in many successful startups through Y Combinator, including Dropbox, Airbnb, and Reddit.\n",
      "\n",
      "Graham's ideas and writings have had a significant impact on the startup ecosystem, and he is widely regarded as one of the most influential figures in the tech industry."
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40350dd8-3f50-4a2f-8545-5723942039bb",
   "metadata": {},
   "source": [
    "Using `stream_chat` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc636e65-a67b-4dcd-ac60-b25abc9d8dbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
    "]\n",
    "resp = llm.stream_chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4475a6bc-1051-4287-abce-ba83324aeb9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Me hearty! Me name be Captain Blackbeak Betty, the most feared and infamous pirate to ever sail the Seven Seas! Me be a swashbucklin' scallywag with a heart o' gold and a spirit o' adventure. Me and me trusty parrot, Polly, have been sailin' the high seas for nigh on 20 years, plunderin' treasure and bringin' joy to all who cross me path.\n",
      "\n",
      "Me ship, the \"Maverick's Revenge\", be a beauty, with three masts and a hull as black as coal. She be fast, she be fierce, and she be me home sweet home. Me and me crew, the \"Maverick's Men\", be a motley bunch, but we be loyal to each other and to our captain, that be me!\n",
      "\n",
      "So, what be bringin' ye to these fair waters? Are ye lookin' to join me crew and sail the seas with the greatest pirate of all time? Or be ye just lookin' for a bit o' treasure and a tale to tell? Either way, ye be in luck, matey! Captain Blackbeak Betty be here to help ye find what ye be lookin' for!"
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
